package com.kpijavatraining6.module2.questions;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Vladislav
 * Date: 08.11.13
 * Time: 0:22
 * To change this template use File | Settings | File Templates.
 */
public class WriteAnswerQuestion extends QuestionStructure{

    protected ArrayList<String> rightAnswers;

    public WriteAnswerQuestion(String newQuestion, /*Topic topic,*/ int questionLevel, String... newRightAnswers) {
        question = newQuestion;
        rightAnswers = new ArrayList<String>();
        for(String answer : newRightAnswers) {
            rightAnswers.add(answer);
        }
        this.topic = topic;
        this.questionLevel = questionLevel;
    }

    public boolean isRightAnswer(String userAnswer) {
        for (String rightAnswer : rightAnswers) {
            if (rightAnswer.equals(userAnswer)) {
                return true;
            }
        }
        return false;
    }

    public void printQuestion() {
        System.out.println("Enter your answer:");
        int i = 1;
        System.out.println(question);
    }

    public void printToFile(BufferedWriter bufferedWriter, String userAnswer) {
        try{

            bufferedWriter.write("2 "+question+" ");
            for (String answer: rightAnswers) {
                bufferedWriter.write(answer+";");
            }
            bufferedWriter.write(" "+userAnswer);
        }   catch (IOException e) {
            System.out.println("an error occurred while saving data");
        }
    }

    public void printToFileOnlyQuestion(BufferedWriter bufferedWriter) {
        try{

            bufferedWriter.write("2 "+question+" ");
            for (String answer: rightAnswers) {
                bufferedWriter.write(answer+";");
            }
        }   catch (IOException e) {
            System.out.println("an error occurred while saving data");
        }
    }
}
